/**
 * http://www.infoq.com/cn/articles/es6-in-depth-modules
 * 模块的使用教程
 */

//存储密码
function set_password (password) {
    sessionStorage.setItem('area_password', password);
}

//获取密码
function get_password () {
    return sessionStorage.getItem('area_password');
}

function set_local_user_info (options) {
    localStorage.setItem('area_user_id', options.id);
    localStorage.setItem('area_user_name', options.user_name);
    localStorage.setItem('area_user_token', options.token);
    localStorage.setItem('area_proxy_id', options.proxy_id);
    localStorage.setItem('area_proxy_data', JSON.stringify(options.proxy_data));
}

//获取用户id
function get_local_user_id () {
    return localStorage.getItem('area_user_id');
}

//获取用户名
function get_local_user_name () {
    return localStorage.getItem('area_user_name');
}

//获取用户token
function get_local_user_token () {
    return localStorage.getItem('area_user_token');
}

//获取用户proxy_id
function get_local_user_proxy () {
    return localStorage.getItem('area_proxy_id');
}

//清除所有本地信息
function clear_local_user_info () {
    localStorage.clear();
    return true;
}

/**时间格式转化 */
function formatTime (date) {
    if (!isNaN(date)) {
        date = new Date(date * 1000);
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var day = date.getDate();

        var hour = date.getHours();
        var minute = date.getMinutes();
        var second = date.getSeconds();

        return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':');
    } else {
        return date;
    }

}

function formatNumber (n) {
    n = n.toString();
    return n[1] ? n : '0' + n;
}

export {
    set_password,
    get_password,
    set_local_user_info,
    get_local_user_id,
    get_local_user_name,
    clear_local_user_info,
    get_local_user_token,
    get_local_user_proxy,
    formatTime
};
